CREATE OR REPLACE FUNCTION evaluate_Referred_ticket_addendum_table_features() RETURNS VOID AS '
DECLARE
 
  v_countLeadTicketsMatched INTEGER;
  v_countLeadTicketsFound INTEGER;
  v_countLeadTicketsTheir INTEGER;
  v_leadRecall REAL;
  v_leadPrecision REAL;


BEGIN


--Query

	--ReferredMetadata 

--select count(*) INTO v_countLeadTicketsFound
--from referredtickets t
--where   t.leadticket is not null and
--t.referredticket is not null and leadticket!=referredticket;

 select count(*) INTO v_countLeadTicketsMatched
 from referredtickets t, addendum_ecs_me_referredtickets_1996_2006 e
 where   t.leadticket is not null and  
 (t.leadticket = e.referredto and t.referredticket = e.ecsticket)
 and (t.leadticket!=t.referredticket) and e.ecsticket!=e.referredto;

select count(*) INTO v_countLeadTicketsTheir
from addendum_ecs_me_referredtickets_1996_2006;



--Calculation

	--ReferredMetadata

v_leadRecall := CAST(v_countLeadTicketsMatched as REAL) / CAST(v_countLeadTicketsTheir as REAL);
--v_leadPrecision := CAST(v_countLeadTicketsMatched as REAL) / CAST(v_countLeadTicketsFound as REAL);



  --Print


  RAISE NOTICE ''==========================================='';
   RAISE NOTICE ''v_countLeadTicketsTheir : : % '',  v_countLeadTicketsTheir;
    RAISE NOTICE ''v_countLeadTicketsMatched : : % '',  v_countLeadTicketsMatched;
  RAISE NOTICE ''ReferredMetadata : recall: % '',  v_leadRecall;
  RAISE NOTICE ''==========================================='';

END;
' LANGUAGE 'plpgsql';

-- SELECT evaluate_Referred_ticket_addendum_table_features();